﻿<?php 
if (!defined('WEB_ROOT')) {
	exit;
}

require_once './library/search_functions.php';

$searchText = (isset($_GET['adsw']) && $_GET['adsw'] != '') ? $_GET['adsw'] : ''; // advance search word
if ($searchText == '')
{
	$searchText = (isset($_GET['sw']) && $_GET['sw'] != '') ? $_GET['sw'] : ''; // advance search word
}
$searchOpt = (isset($_GET['adsopt']) && $_GET['adsopt'] != '') ? $_GET['adsopt'] : 'td'; 
$searchCat = (isset($_GET['adscat']) && $_GET['adscat'] != '') ? (int)$_GET['adscat'] : 0; 

if ($searchText != '')
{
	$sql = getSearchItemQuery($searchText, $searchCat, $searchOpt);
}
else
{
	$sql = "";
}

//$result = dbQuery(getPagingQuery($sql, NUM_ITEMS_PER_PAGE));
#echo 'AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA '.$sql . 'AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA AAA ';
$sCount = 0;
$pagingLink = '';
if ($sql != '')
{
	$sResult = dbQuery(getPagingQuery($sql, NUM_ITEMS_PER_PAGE));
	$sCount = dbNumRows($sResult);
	$pagingLink = getPagingLink($sql, NUM_ITEMS_PER_PAGE, "view=search&adsw=$searchText&adsopt=$searchOpt&adscat=$searchCat");
}


//$result = array();
//$result[] = array('tenbb'=>'ten bai bao', 'tomtat_bb'=>'tom tat bai bao');



$scatMap = array(TABLE_BAIBAO_ID=>'bb', TABLE_DETAI_ID=>'dt', TABLE_SEMINAR_ID=>'sm');
//echo $count;
//var_dump($result);
?>
<script>

</script>
<div id="cat_searchResult" class="cat-widget">
	<h3>
		<a title="" href="">Tìm kiếm</a>
		<a class="minimize"></a>
		<form class="searchform2" name="adSearchForm" method="get" action="javascript:viewAdvanceSearch();">
				<select id="optSearch" name="optSearch" class="fixeddropdown" onchange="">
					<option value="td" <? echo ($searchOpt == 'td' ? 'selected=true' : ''); ?>>Tìm tương đối</option>
					<option value="cx" <? echo ($searchOpt == 'cx' ? 'selected=true' : ''); ?>>Tìm chính xác</option>
				</select>
				<input type="text" value="<?php echo $searchText; ?>" id="adSearchText" name="adSearchText" class="searchfield"></input>
				<a style="margin-left:5px;">Tìm trong danh mục</a>
				<select id="optSearchCat" name="optSearchCat" class="fixeddropdown" onchange="">
					<option value="0" <? echo ($searchCat == 0 ? 'selected=true' : ''); ?> >Tất cả</option>
					<option value="1" <? echo ($searchCat == 1 ? 'selected=true' : ''); ?> >Bài báo</option>
					<option value="2" <? echo ($searchCat == 2 ? 'selected=true' : ''); ?> >Đề tài Khoa học</option>
					<option value="3" <? echo ($searchCat == 3 ? 'selected=true' : ''); ?> >Seminar</option>
				</select>
				<input type="submit" value="Tìm kiếm" class="searchbutton" ></input>
		</form>
	</h3>
	<div class="container">
		<div class="mpost">
			<ul class="list_articles">
			<?php for ($sItem = 0; $sItem < $sCount; $sItem++) { 
				$row = dbFetchAssoc($sResult);
				$table_id = $row['table_id'];
				$item_id = $row['item_id'];
				$ten = $row['ten'];
				$tomtat = $row['tomtat'];
			?>
			<li class="list_articles_item">
				<div class="thumb">
					<a href="javascript:viewArticle('<?php echo $scatMap[$table_id]; ?>', <?php echo $item_id; ?>)">
						<img src="images/common/article_default.jpg" width="100" height="75"></img>
					</a>
				</div>
				<div class="info">
					<a class="dark_perm" title="<?php echo $ten; ?>" href="javascript:viewArticle('<?php echo $scatMap[$table_id]; ?>', <?php echo $item_id; ?>)">
						<?php echo $ten; ?>
					</a>
					<p><?php echo $tomtat; ?>...</p><div class="clear"></div>
					<ul class="ex">
						<li class="cont"><a href="javascript:viewArticle('<?php echo $scatMap[$table_id]; ?>', <?php echo $item_id; ?>)">Ðọc tiếp</a></li>
					</ul><div class="clear"></div>
				</div>
			</li>
			<?php } ?>
			</ul>
			
			<?php if ($pagingLink != '') { ?>
			<div class="pagination-bar">
				<div class="pagination"><?php echo $pagingLink; ?></div>
				<div class="clear"></div>
			</div>
			<?php } ?>
		</div>
	</div>
	<div class="footer"></div>
	<div class="clear"></div>
</div>
